_package("alz.core");

_import("alz.core.EventTarget");

/**
 * 应用管理者
 */
_class("AppManager", EventTarget, function(){
	this._init = function(){
		_super._init.call(this);
		this._appConfs = {};         //应用配置
		this._activeAppConf = null;  //当前的应用配置
		this._hash = {};             //所有应用的哈希表
		this._activeApp = null;      //当前应用
		this._mainApp = null;        //主应用
	};
	this.dispose = function(){
		this._mainApp = null;
		if(this._activeApp){
			//this._activeApp.dispose();
			this._activeApp = null;
		}
		for(var k in this._hash){
			this._hash[k].dispose();
			delete this._hash[k];
		}
		this._activeAppConf = null;
		for(var k in this._appConfs){
			delete this._appConfs[k];
		}
		_super.dispose.apply(this);
	};
	this.getActiveAppConf = function(){
		return this._activeAppConf;
	};
	this.getActiveApp = function(){
		return this._activeApp;
	};
	this.setActiveApp = function(v){
		this._activeApp = v;
	};
	this.getMainApp = function(){
		if(!this._mainApp){
			this._mainApp = new function(){
				this._tplData = runtime.getTplData("aui");
				this.getTpl = function(name){
					return this._tplData[name];
				};
			};
		}
		return this._mainApp;
	};
	/**
	 * 注册一个应用
	 */
	this.regApp = function(name, conf){
		if(name in this._appConfs){
			runtime.error("[AppManager::regApp]应用重名name=" + name + "，注册失败");
			return;
		}
		this._appConfs[name] = conf;
		if(conf.active){
			this._activeAppConf = conf;
		}
	};
	/**
	 * 创建一个应用
	 * @param {String} name 应用标识名
	 * @param {String} className 应用类的类名
	 * @param {Object} params 传递给应用的参数
	 * @return {Application}
	 */
	this.createApp = function(name, className, params){
		var clazz = (className.indexOf(".") != -1 ? __classes__ : __context__)[className];
		//var app = new clazz();
		//if(!app._mainwindow){
		//	window.parent.runtime.getActiveApp()._pane.setApp(app);
		//}
		var app = new clazz();
		this._hash["default"] = app;
		app.setConf(this._appConfs[name]);  //设置app配置信息
		if(params) app.setParams(params);   //设置参数
		//app.init();
		this.setActiveApp(app);
		return app;
	};
	this.onOffline =
	this.onSuspend =
	this.onResume = function(ev){
		for(var k in this._hash){
			this._hash[k].fireEvent(ev);
		}
	};
});